package parser;

import antlr.RecognitionException;
import antlr.Token;
import java.util.*;

class Except {
    
    List<Expr> types;
    Expr var;
    
    Except(List<Expr> types, Expr var) {
        this.types = types;
        this.var = var;
    }
    
    void write(ByteStream out) throws RecognitionException {
        if (types == null) {
            out.write(Opcodes.DROP);
        } else {
            List<Patch> patches = new ArrayList<Patch>();
            for (Expr t : types) {
                out.write(Opcodes.COPY);
                t.write(out);
                out.write(Opcodes.TYPE_CHECK);
                patches.add(out.branch(Opcodes.ON_TRUE));
            }
            out.write(Opcodes.RERAISE);
            for (Patch p: patches) {
                p.patch(out);
            }
            if (var == null)
                out.write(Opcodes.DROP);
            else
                var.writeLHS(out);
        }
    }
}
